package cn.kgc.math.work01;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GoodsTest {
    private static List<Goods>goodsList =new ArrayList<Goods>(){{
        add(new Goods(1,"电风扇",1224.23));
        add(new Goods(2,"洗衣机",4500.60));
        add(new Goods(3,"电视机",8800.90));
        add(new Goods(4,"电冰箱",5000.88));
        add(new Goods(5,"空调机",4456.00));
    }};



    public static String format(double value){ //23,528,345.83
        String strValue = Double.toString(value);
        StringBuffer buffer = new StringBuffer(strValue);
        int index = buffer.indexOf(".");
        for (int i = index - 3; i > 0 ; i -= 3) {
            buffer.insert(i,",");
        }
        return buffer.toString();
    }

    public static double pay(int goodsId,int qty){ //quantity  数量
        Goods goods = null;
        double total = 0.0;

        for (int i = 0,size = goodsList.size(); i < size; i++) {
            goods = goodsList.get(i);
            if(goodsId == goods.getGoodsId()){
                total = qty * goods.getGoodsPrice();
                break;
            }
        } //end for
        return total;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("*******欢迎进入商品批发城********");
        for (Goods goods : goodsList) {
            System.out.println(goods.getGoodsId() + "\t" + goods.getGoodsName() + "\t" + format(goods.getGoodsPrice()));
        }

        System.out.println("****************************************");
        System.out.println("请输入您批发的商品编号:");
        int id = input.nextInt();
        System.out.println("请输入您想要批发商品数量:");
        int qty = input.nextInt();
        double totalPrice = pay(id,qty);
        System.out.println("您需要付款为：" + format(totalPrice));
    }

}
